<template>
  <div class="hl-icon-demo">
    <el-scrollbar>
      <ul class="icon_ul">
        <li v-for="(item, index) in icons" :key="item" @click="copyDomText('icon-' + item)" class="hl-icon-li">
          <div class="items" @mouseenter="handleMouseenter(index)" @mouseleave="handleMouseleave(index)">
            <div class="icon_svg">
              <el-icon :size="24">
                <SvgIcon :icon-class="'icon-' + item" :size="'24px'"
                  :style="btnIndex == index ? 'transform: scale(1.2);transition: all .3s ease;' : ''"></SvgIcon>
              </el-icon>
            </div>
          </div>
          <div class="iconName ellipsis">{{ item }}</div>
        </li>
      </ul>
    </el-scrollbar>
  </div>
</template>
<script lang="ts" setup>
import url from '../../assets/fonts/iconfont.svg'
import getText from './icon'
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
interface EmitType {
  (e: 'selectchangeIcon', val: string): void
}
const emit = defineEmits<EmitType>()
const icons = ref<any>([])
const btnIndex = ref(1000)
const handleMouseenter = (index: number) => {
  btnIndex.value = index
}
const handleMouseleave = (index: number) => {
  // btnIndex.value = choiceIndex.value
  // console.log(index)
}
const onCopy = (e: any) => {
  ElMessage(`你已复制[${e.text}]到剪切板`)
}
const onError = (e: any) => {
  ElMessage('复制失败，请重试')
}
const copyDomText = (val: string): void => {
  // 获取需要复制的元素以及元素内的文本内容
  const text = val
  // 添加一个input元素放置需要的文本内容
  const input = document.createElement('input')
  input.value = text
  document.body.appendChild(input)
  // 选中并复制文本到剪切板
  input.select()
  document.execCommand('copy')
  // 移除input元素
  document.body.removeChild(input)
  // ElMessage.success(`你已复制[${text}]到剪切板`)
  ElMessage.success(`你已复制[${text}]到剪切板`)
  emit('selectchangeIcon', text)
  // console.log(`你已复制[${text}]到剪切板`)
}
onMounted(() => {
  getText(url).then((data: any) => {
    const txt = data
    if (!txt || txt === '') return
    const iconArr = txt.match(/glyph-name="(\S*)/gm)
    // console.log(iconArr)
    iconArr.forEach((v: any) => {
      icons.value.unshift(
        v
          .split('=')[1]
          .replace('"', '')
          .replace('"', '')
      )
    })
  })

  document.addEventListener('copy', (evt: any) => {
    // console.log(evt.target.value)
  })
})
onUnmounted(() => { })
</script>
<style lang="scss" scoped>
.hl-icon-demo {
  width: 100%;
  height: 100%;
  background-color: var(--el-color-white);
  // overflow-y: scroll;
}

ul li {
  list-style: none;
}

.icon_ul {
  display: flex;
  flex-wrap: wrap;
  padding: 0 20px;

  .hl-icon-li {
    width: 80px;
    margin: 10px;
    text-align: center;

    .items {
      width: 100%;
      border-radius: 4px;
      border: 1px solid #EEEEEE;
      height: 50px;
      text-align: center;
      cursor: pointer;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-wrap: wrap;
    }

    .iconName {
      font-size: 13px;
      margin-top: 5px;
    }

    .items:hover {
      box-shadow: 0px 0px 12px rgba(0, 0, 0, .12);
    }
  }
}
</style>
